﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.OA.Interface.CRM;
using NetDon.Utilities;
using NetDon.WebControls;

namespace NetDon.OA.Portal.Components
{
    public partial class CustomerSearchPopup : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CancelButton = btnCancel;
                PagerCustomers.PerformQuery();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            PagerCustomers.PerformQuery();
        }

        protected void PagerCustomers_PerformGetPageData(object sender, PerformGetPageDataEventArgs e)
        {
            var service = ServiceContainer.GetService<ICustomerInfoService>();
            e.DataSource = service.SearchCustomerInfoDatasByCondition((CustomerInfoSearchCriteria)e.Condition, gridCustomers.SortExpression,
                                                      gridCustomers.SortDirection, e.PageSize, e.PageIndex);
        }

        protected void PagerCustomers_PerformGetRecordCount(object sender, PerformGetRecordCountEventArgs e)
        {
            var service = ServiceContainer.GetService<ICustomerInfoService>();
            e.RecordCount = service.GetCustomerInfoCount((CustomerInfoSearchCriteria)e.Condition);
        }

        protected void PagerCustomers_PerformGetCondition(object sender, PerformGetConditionEventArgs e)
        {
            var criteria = new CustomerInfoSearchCriteria
            {
                CustomerCode = txtCustomCode.Text.Trim(),
                CustomerName = txtCustomName.Text.Trim(),
                ContactName = txtContactName.Text.Trim()
            };

            e.Condition = criteria;
        }

        protected void btnChoose_Click(object sender, EventArgs e)
        {
            var selectedUsers = CheckItemSelection();
            if (selectedUsers.Count > 0)
            {
                var serializer = new DataContractJsonSerializer(typeof(IList<CustomerData>));
                var stream = new MemoryStream();
                serializer.WriteObject(stream, selectedUsers);
                stream.Position = 0;
                var sr = new StreamReader(stream);
                string resultStr = sr.ReadToEnd();
                sr.Close();
                stream.Close();
                CloseWindow(resultStr);
            }
            else
            {
                base.Alert("请选择用户.");
                return;
            }
        }

        protected void radioSelected_CheckedChanged(object sender, EventArgs e)
        {
            var rdb = sender as RadioButton;

            foreach (GridViewRow row in gridCustomers.Rows)
            {
                var radioButton = row.FindControl("radioSelected") as RadioButton;
                if (radioButton == rdb) continue;
                radioButton.Checked = false;
            }
        }


        public IList<CustomerData> CheckItemSelection()
        {
            var selectedUsers = new List<CustomerData>();

            foreach (GridViewRow row in gridCustomers.Rows)
            {
                var radioButton = (RadioButton)row.FindControl("radioSelected");
                if (radioButton != null)
                {
                    if (radioButton.Checked)
                    {
                        var customerId = row.Attributes["CustomerId"].ToGuid();
                        var customerCode = (row.Attributes["CustomerCode"]);
                        var customerName = (row.Attributes["CustomerName"]);

                        selectedUsers.Add(new CustomerData()
                        {
                            CustomerId = customerId,
                            CustomerCode = customerCode,
                            CustomerName = customerName
                        });
                        break;
                    }
                }
            }



            return selectedUsers;
        }

        protected void gridCustomers_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var entity = (e.Row.DataItem);
                if (entity != null)
                {
                    e.Row.Attributes["CustomerId"] = Convert.ToString(DataBinder.Eval(entity, "CustomerId"));
                    e.Row.Attributes["CustomerCode"] = Convert.ToString(DataBinder.Eval(entity, "CustomerCode"));
                    e.Row.Attributes["CustomerName"] = Convert.ToString(DataBinder.Eval(entity, "CustomerName"));
                }
            }
        }
    }

    [DataContract]
    public class CustomerData
    {
        [DataMember]
        public Guid CustomerId { get; set; }
        [DataMember]
        public string CustomerCode { get; set; }
        [DataMember]
        public string CustomerName { get; set; }
    }
}